package com.cherish.web.menu.entity;

import com.cherish.web.user.entity.RouterVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * Author:MQ
 * Date:2024/1/310:36
 * 生成菜单树
 */
public class MakeMenuTree {
    public static List<Menu> makeTree(List<Menu> menuList, Long pid){
        List<Menu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null &&  item.getFParentId() == pid)
                .forEach(item -> {
                    Menu menu = new Menu();
                    BeanUtils.copyProperties(item, menu);
                    List<Menu> children = makeTree(menuList,item.getFId());
                    menu.setChildren(children);
                    list.add(menu);
                });
        return list;
    }
    public static List<RouterVO> makeRouter(List<Menu> menuList, Long pid){
        List<RouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getFParentId() == pid)
                .forEach(item -> {
                    RouterVO router = new RouterVO();
                    router.setFName(item.getFName());
                    router.setFPath(item.getFPath());
                    if(item.getFParentId() == 0L){
                        router.setFComponent("Layout");
                        router.setFAlwaysShow(true);
                    }else{
                        router.setFComponent(item.getFUrl());
                        router.setFAlwaysShow(false);
                    }
                    //设置meta
                    router.setFMeta(router.new Meta(
                            item.getFLabel(),
                            item.getFIcon(),
                            item.getFCode().split(",")
                    ));
                    //设置children,每一项的下级
                    List<RouterVO> children = makeRouter(menuList, item.getFId());
                    router.setChildren(children);
                    list.add(router);
                });
        return list;
    }
    // 递归生成路由
    public static List<RouterVO> recursionForRoutes(Long parentId, List<Menu> menuList) {
        List<RouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
                .filter(menu -> menu.getFParentId()== parentId)
                .forEach(menu -> {
                    RouterVO routerVO = new RouterVO();
                    routerVO.setFName(menu.getFName());
                    routerVO.setFPath(menu.getFPath());
                    if (parentId == 0L) {
                        routerVO.setFComponent("Layout");
                        routerVO.setFAlwaysShow(true);
                    } else {
                        routerVO.setFComponent(menu.getFUrl());
                        //设为true时，将会展示为上级菜单
                        routerVO.setFAlwaysShow(false);
                    }
                    routerVO.setFMeta(routerVO.new Meta(
                            menu.getFLabel(),
                            menu.getFIcon(),
                            menu.getFCode().split(",")
                    ));
                    // 菜单显示隐藏
                    List<RouterVO> children = recursionForRoutes(menu.getFId(), menuList);
                    routerVO.setChildren(children);
                    list.add(routerVO);
                }));
        return list;
    }
}
